Refactor tests and add test for traverse_commit
authorFelix Krull <f_krull@gmx.de>
Tue, 21 May 2019 20:58:40 +0000 (22:58 +0200)
committerColin Walters <walters@verbum.org>
Fri, 6 May 2022 16:53:53 +0000 (12:53 -0400)
rust-bindings/rust/Cargo.toml
rust-bindings/rust/tests/data/test.tar [new file with mode: 0644]
rust-bindings/rust/tests/repo.rs [new file with mode: 0644]
rust-bindings/rust/tests/roundtrip.rs [deleted file]
rust-bindings/rust/tests/util/mod.rs [new file with mode: 0644]

index d8fbc959ec1110313cd9f0c4e1bd12a07cda1434..33f8848adb94462305c566c4bed3ebc5d94bed97 100644 (file)
@@ -39,6 +39,7 @@ gio-sys = "0.8.0"
 ostree-sys = { version = "0.3.0", path = "sys" }
 
 [dev-dependencies]
+maplit = "1.0.1"
 tempfile = "3"
 
 [features]
diff --git a/rust-bindings/rust/tests/data/test.tar b/rust-bindings/rust/tests/data/test.tar
new file mode 100644 (file)
index 0000000..fd39eda
Binary files /dev/null and b/rust-bindings/rust/tests/data/test.tar differ
diff --git a/rust-bindings/rust/tests/repo.rs b/rust-bindings/rust/tests/repo.rs
new file mode 100644 (file)
index 0000000..c3a773c
--- /dev/null
@@ -0,0 +1,63 @@
+extern crate gio;
+extern crate glib;
+extern crate ostree;
+extern crate tempfile;
+#[macro_use]
+extern crate maplit;
+
+mod util;
+use util::*;
+
+use gio::NONE_CANCELLABLE;
+use ostree::prelude::*;
+use ostree::{ObjectName, ObjectType};
+
+#[test]
+fn should_commit_content_to_repo_and_list_refs_again() {
+    let test_repo = TestRepo::new();
+
+    let mtree = create_mtree(&test_repo.repo);
+    let checksum = commit(&test_repo.repo, &mtree, "test");
+
+    let repo = ostree::Repo::new_for_path(test_repo.dir.path());
+    repo.open(NONE_CANCELLABLE).expect("OSTree test_repo");
+    let refs = repo
+        .list_refs(None, NONE_CANCELLABLE)
+        .expect("failed to list refs");
+    assert_eq!(1, refs.len());
+    assert_eq!(checksum, refs["test"]);
+}
+
+#[test]
+fn should_traverse_commit() {
+    let test_repo = TestRepo::new();
+    let checksum = test_repo.test_commit();
+
+    let objects = test_repo
+        .repo
+        .traverse_commit(&checksum, -1, NONE_CANCELLABLE)
+        .expect("traverse commit");
+
+    assert_eq!(
+        hashset!(
+            ObjectName::new(
+                "89f84ca9854a80e85b583e46a115ba4985254437027bad34f0b113219323d3f8",
+                ObjectType::File
+            ),
+            ObjectName::new(
+                "5280a884f930cae329e2e39d52f2c8e910c2ef4733216b67679db32a2b56c4db",
+                ObjectType::DirTree
+            ),
+            ObjectName::new(
+                "c81acde323d73f8639fc84f1ded17bbafc415e645f845e9f3b16a4906857c2d4",
+                ObjectType::DirTree
+            ),
+            ObjectName::new(
+                "ad49a0f4e3bc165361b6d17e8a865d479b373ee67d89ac6f0ce871f27da1be6d",
+                ObjectType::DirMeta
+            ),
+            ObjectName::new(checksum, ObjectType::Commit)
+        ),
+        objects
+    );
+}
diff --git a/rust-bindings/rust/tests/roundtrip.rs b/rust-bindings/rust/tests/roundtrip.rs
deleted file mode 100644 (file)
index 6e273b9..0000000
+++ /dev/null
@@ -1,79 +0,0 @@
-extern crate gio;
-extern crate glib;
-extern crate ostree;
-extern crate tempfile;
-
-use gio::NONE_CANCELLABLE;
-use glib::prelude::*;
-use ostree::prelude::*;
-use ostree::RepoFile;
-use std::fs;
-use std::io;
-use std::io::Write;
-
-fn create_repo(repodir: &tempfile::TempDir) -> Result<ostree::Repo, glib::Error> {
-    let repo = ostree::Repo::new_for_path(repodir.path());
-    repo.create(ostree::RepoMode::Archive, NONE_CANCELLABLE)?;
-    Ok(repo)
-}
-
-fn create_test_file(treedir: &tempfile::TempDir) -> Result<(), io::Error> {
-    let mut testfile = fs::File::create(treedir.path().join("test.txt"))?;
-    write!(testfile, "test")?;
-    Ok(())
-}
-
-fn create_mtree(
-    treedir: &tempfile::TempDir,
-    repo: &ostree::Repo,
-) -> Result<ostree::MutableTree, glib::Error> {
-    let gfile = gio::File::new_for_path(treedir.path());
-    let mtree = ostree::MutableTree::new();
-    repo.write_directory_to_mtree(&gfile, &mtree, None, NONE_CANCELLABLE)?;
-    Ok(mtree)
-}
-
-fn commit_mtree(repo: &ostree::Repo, mtree: &ostree::MutableTree) -> Result<String, glib::Error> {
-    repo.prepare_transaction(NONE_CANCELLABLE)?;
-    let repo_file = repo
-        .write_mtree(mtree, NONE_CANCELLABLE)?
-        .downcast::<RepoFile>()
-        .unwrap();
-    let checksum = repo
-        .write_commit(
-            None,
-            "Test Commit".into(),
-            None,
-            None,
-            &repo_file,
-            NONE_CANCELLABLE,
-        )?
-        .to_string();
-    repo.transaction_set_ref(None, "test", checksum.as_str().into());
-    repo.commit_transaction(NONE_CANCELLABLE)?;
-    Ok(checksum)
-}
-
-fn open_repo(repodir: &tempfile::TempDir) -> Result<ostree::Repo, glib::Error> {
-    let repo = ostree::Repo::new_for_path(repodir.path());
-    repo.open(NONE_CANCELLABLE)?;
-    Ok(repo)
-}
-
-#[test]
-fn should_commit_content_to_repo_and_list_refs_again() {
-    let repodir = tempfile::tempdir().unwrap();
-    let treedir = tempfile::tempdir().unwrap();
-
-    let repo = create_repo(&repodir).expect("failed to create repo");
-    create_test_file(&treedir).expect("failed to create test file");
-    let mtree = create_mtree(&treedir, &repo).expect("failed to build mtree");
-    let checksum = commit_mtree(&repo, &mtree).expect("failed to commit mtree");
-
-    let repo = open_repo(&repodir).expect("failed to open repo");
-    let refs = repo
-        .list_refs(None, NONE_CANCELLABLE)
-        .expect("failed to list refs");
-    assert_eq!(refs.len(), 1);
-    assert_eq!(refs["test"], checksum);
-}
diff --git a/rust-bindings/rust/tests/util/mod.rs b/rust-bindings/rust/tests/util/mod.rs
new file mode 100644 (file)
index 0000000..923a2f9
--- /dev/null
@@ -0,0 +1,67 @@
+use gio::NONE_CANCELLABLE;
+use glib::prelude::*;
+use glib::GString;
+use ostree::prelude::*;
+use std::path::Path;
+
+#[derive(Debug)]
+pub struct TestRepo {
+    pub dir: tempfile::TempDir,
+    pub repo: ostree::Repo,
+}
+
+impl TestRepo {
+    pub fn new() -> TestRepo {
+        TestRepo::new_with_mode(ostree::RepoMode::BareUser)
+    }
+
+    pub fn new_with_mode(repo_mode: ostree::RepoMode) -> TestRepo {
+        let dir = tempfile::tempdir().expect("temp repo dir");
+        let repo = ostree::Repo::new_for_path(dir.path());
+        repo.create(repo_mode, NONE_CANCELLABLE)
+            .expect("OSTree repo");
+        TestRepo { dir, repo }
+    }
+
+    pub fn test_commit(&self) -> GString {
+        let mtree = create_mtree(&self.repo);
+        commit(&self.repo, &mtree, "test")
+    }
+}
+
+pub fn create_mtree(repo: &ostree::Repo) -> ostree::MutableTree {
+    let mtree = ostree::MutableTree::new();
+    let file = gio::File::new_for_path(
+        Path::new(env!("CARGO_MANIFEST_DIR"))
+            .join("tests")
+            .join("data")
+            .join("test.tar"),
+    );
+    repo.write_archive_to_mtree(&file, &mtree, None, true, NONE_CANCELLABLE)
+        .expect("test mtree");
+    mtree
+}
+
+pub fn commit(repo: &ostree::Repo, mtree: &ostree::MutableTree, ref_: &str) -> GString {
+    repo.prepare_transaction(NONE_CANCELLABLE)
+        .expect("prepare transaction");
+    let repo_file = repo
+        .write_mtree(mtree, NONE_CANCELLABLE)
+        .expect("write mtree")
+        .downcast::<ostree::RepoFile>()
+        .unwrap();
+    let checksum = repo
+        .write_commit(
+            None,
+            "Test Commit".into(),
+            None,
+            None,
+            &repo_file,
+            NONE_CANCELLABLE,
+        )
+        .expect("write commit");
+    repo.transaction_set_ref(None, ref_, checksum.as_str().into());
+    repo.commit_transaction(NONE_CANCELLABLE)
+        .expect("commit transaction");
+    checksum
+}